import {createRouter, createWebHistory} from 'vue-router'
import {getUserInfo} from '@/utils/storage'
import {ElMessage} from "element-plus";

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: '/login',
            name: 'login',
            meta: {title: '登录', adminId: 1},
            component: () => import('@/views/Login/login.vue')
        },
        {
            path: '/',
            name: 'layout',
            meta: {title: '个人中心', adminId: 1},
            component: () => import('@/views/Layout/layout.vue'),
            children: [
                {
                    path: '/home',
                    name: 'home',
                    meta: {title: '后台首页', adminId: 1},
                    component: () => import('@/views/Home/home.vue')
                },
                {
                    path: '/order',
                    name: 'order',
                    meta: {title: '订单管理', adminId: 1},
                    component: () => import('@/views/Order/order.vue')
                },
                {
                    path: '/good',
                    name: 'good',
                    meta: {title: '商品管理', adminId: 1},
                    children: [
                        {
                            path: '/good/goodList',
                            name: 'goodList',
                            meta: {title: '商品列表', adminId: 1},
                            component: () => import('@/views/Goods/goodsList.vue')
                        },
                        {
                            path: '/good/goodAdd',
                            name: 'goodAdd',
                            meta: {title: '商品添加', adminId: 0},
                            component: () => import('@/views/Goods/goodsAdd.vue')
                        },
                        {
                            path: '/good/goodType',
                            name: 'goodType',
                            meta: {title: '商品类型', adminId: 1},
                            component: () => import('@/views/Goods/goodsType.vue')
                        },
                    ]
                },
                {
                    path: '/storeManagement',
                    name: 'storeManagement',
                    meta: {title: '店铺管理', adminId: 0},
                    component: () => import('@/views/Shop/storeManagement.vue')
                },

                {
                    path: '/account',
                    name: 'account',
                    meta: {title: '账号管理', adminId: 1},
                    children: [
                        {
                            path: '/account/accountList',
                            name: 'accountList',
                            meta: {title: '账号列表', adminId: 0},
                            component: () => import('@/views/Account/accountList.vue')
                        },
                        {
                            path: '/account/accountAdd',
                            name: 'accountAdd',
                            meta: {title: '添加账号', adminId: 0},
                            component: () => import('@/views/Account/accountAdd.vue')
                        },
                        {
                            path: '/account/accountChange',
                            name: 'accountChange',
                            meta: {title: '修改密码', adminId: 0},
                            component: () => import('@/views/Account/accountChange.vue')
                        },
                    ]
                },
                {
                    path: '/statistics',
                    name: 'statistics',
                    meta: {title: '销售统计', adminId: 1},
                    children: [
                        {
                            path: '/statistics/orderStatistics',
                            name: 'orderStatistics',
                            meta: {title: '订单统计', adminId: 1},
                            component: () => import('@/views/Sales/orderStatistics.vue')
                        },
                        {
                            path: '/statistics/shopStatistics',
                            name: 'shopStatistics',
                            meta: {title: '商品统计', adminId: 1},
                            component: () => import('@/views/Sales/shopStatistics.vue')
                        },
                    ]
                },


            ]
        }
    ]
})

//前置路由守卫
router.beforeEach((to, from, next) => {
    //获取用户信息 判断是否登录
    const userInfo = getUserInfo()
    //如果没有登录 且不在登录页就重定向到登录页
    if (!userInfo.id) {
        if (to.fullPath != '/login') {
            next('/login')
            console.log('没有登录')
        } else {
            //如果在登录页就放行
            next()
            console.log('在登录页')
        }
    } else {
        //获取用户权限
        const role = Number(userInfo.adminId)

        //如果有登录 且在登录页就重定向到首页
        if (to.fullPath == '/login') {
            next('/')

        } else {
            //如果用户在主页 判断用户是否有访问权限

            //当前页的权限
            const pageRole: any = to.meta.adminId
            ////如果用户没有访问权限就重定向到首页

            if (!(pageRole >= role)) {
                //如果用户有访问权限就放行
                ElMessage.error('你没有访问权限')
                next('/')
            } else {
                console.log('有访问权限')
                next()
            }
        }
    }

})


//解决 在首页时点击面包屑首页报错问题
const originalPush = createRouter.prototype.push;
createRouter.prototype.push = function push(location: any) {
    return originalPush.call(this, location).catch((err: any) => {
        return err
    })
}
export default router
